package com.example.xwadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.xw.common.entity.Admin;
import com.example.xw.common.entity.User;
import com.example.xw.common.entity.Weibo;
import com.example.xw.common.service.AdminService;
import com.example.xw.common.service.CommentService;
import com.example.xw.common.service.UserService;
import com.example.xw.common.service.WeiboService;
import com.example.xw.common.vo.Result;
import com.example.xwadmin.mapper.AdminMapper;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController("myAdminServiceImpl")
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result<Admin> register(Admin admin) {
        String hashpwd= BCrypt.hashpw(admin.getLoginPwd(), BCrypt.gensalt());
        System.out.println(hashpwd);//发现相同的密码，每次都显示不同的值
        admin.setLoginPwd(hashpwd);
        adminMapper.insert(admin);
        return new Result<>(2000,admin,"注册成功");
    }

    @Override
    public Result<Admin> login(String loginname, String loginpwd) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.lambda().eq(Admin::getLoginName,loginname);
        Admin admin = adminMapper.selectOne(qw);
        if(admin==null)
                return new Result<>(401,null,"用户名不存在");
        if (BCrypt.checkpw(loginpwd,admin.getLoginPwd()))
            return new Result<>(200,admin,"登录成功");
        else
            return new Result<>(401,null,"密码错误");
    }
}
